What is loose-envify?
The loose-envify npm package is a fast (loose) selective process.env replacer using js-tokens instead of an AST. It is commonly used to replace process.env.NODE_ENV with a static value allowing for dead code elimination in browser bundles.
What are loose-envify's main functionalities?
Environment Variable Replacement
Replaces instances of process.env.NODE_ENV with a user-defined string, allowing conditional code to be statically evaluated and potentially removed by minifiers.
"if (process.env.NODE_ENV !== 'production') { console.log('Debug info') }"
Other packages similar to loose-envify
envify
envify is similar to loose-envify but performs a full AST analysis which makes it more accurate but slower. It replaces environment variables in the same way.
dotenv
dotenv loads environment variables from a .env file into process.env, but it does not replace them in the code during the build process like loose-envify does.
cross-env
cross-env allows you to set and use environment variables across platforms but does not replace them in the code. It's used more for setting up the environment rather than for code builds.
loose-envify
Fast (and loose) selective process.env
replacer using js-tokens instead of an AST. Works just like envify but much faster.
Gotchas
Usage/Options
loose-envify has the exact same interface as envify, including the CLI.
Benchmark
envify:
$ for i in {1..5}; do node bench/bench.js 'envify'; done
708ms
727ms
791ms
719ms
720ms
loose-envify:
$ for i in {1..5}; do node bench/bench.js '../'; done
51ms
52ms
52ms
52ms
52ms